#include <stdio.h>
#include <arpa/inet.h>
#include <stdlib.h>

u_int64_t htonll(u_int64_t _netlong){
    //判断大小端
    int flag = -1;
    int tmp = 0x12345678;
    //大端直接返回
    if(*(short*)&tmp != 0x5678){
        return _netlong;
    }
    //把long从小端转为大端
    u_int32_t tmp_h = htonl(_netlong >> 32);
    u_int32_t tmp_l = htonl((u_int32_t)(_netlong & 0x00000000ffffffff));
    _netlong = 0;
    _netlong = (_netlong | tmp_h) | ((_netlong | tmp_l) << 32);
    return _netlong;
}

u_int64_t ntohll(u_int64_t _netlong){
    //判断大小端
    int flag = -1;
    int tmp = 0x12345678;
    //大端直接返回
    if(*(short*)&tmp != 0x5678){
        return _netlong;
    }
    //把long从大端转为小端
    u_int32_t tmp_h = ntohl(_netlong >> 32);
    u_int32_t tmp_l = ntohl(_netlong << 32 >> 32);
    _netlong = 0;
    _netlong = (_netlong | tmp_h) | ((_netlong | tmp_l) << 32);
    return _netlong;
}

int main(int argc, char const *argv[])
{
    long data = 0x1234567887654321;
    data = htonll(data);
    data = ntohll(data);
    printf("%lx\n",data);
    return 0;
}
